//
//  JQFYUMShare.m
//  JQFY
//
//  Created by 代辉 on 2017/9/8.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYUMShare.h"
#import "JQFYMusicPlayer.h"

#define USHARE_DEMO_APPKEY    @"59e59ea3aed1797139001035"

@implementation JQFYUMShare

- (UMSocialUserInfoResponse *)userInfo
{
    if (_userInfo == nil) {
        //显示分享面板
        [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
            // 根据获取的platformType确定所选平台进行下一步操作
            NSLog(@"platformType:%zd",platformType);
            
            [self getUserInfoForPlatform:platformType];
        }];
    }
    
    
    return _userInfo;
}

- (void)openShareWebPage
{
    //显示分享面板
//    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_Qzone),@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine)]];
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine)]];
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        // 根据获取的platformType确定所选平台进行下一步操作
        [self shareWebPageToPlatformType:platformType];
    }];
}

#pragma mark - 分享音乐 -
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
     //创建网页内容对象
//     NSString* thumbURL =  __Player__.playingSong.music_cover_path;
     UIImage *thumbURL = [UIImage imageNamed:@"setting_logo"];
     UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:[NSString stringWithFormat:@"%@",__Player__.playingSong.music_name] descr:[NSString stringWithFormat:@"%@",__Player__.playingSong.music_auth] thumImage:thumbURL];
     //设置网页地址
     shareObject.webpageUrl = __Player__.playingSong.music_share;
     
     //分享消息对象设置分享内容对象
     messageObject.shareObject = shareObject;
     /*
    //创建网页内容对象
    NSString* thumbURL = __Player__.playingSong.music_share;
    
   UMShareMusicObject *shareObject = [UMShareMusicObject shareObjectWithTitle:[NSString stringWithFormat:@"%@",__Player__.playingSong.music_name] descr:[NSString stringWithFormat:@"%@",__Player__.playingSong.music_auth] thumImage:thumbURL];
    //设置网页地址
    shareObject.musicUrl = __Player__.playingSong.music_share;
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    */
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                NSLog(@"分享结构:::: == %@ === %@",resp.message,resp.originalResponse);
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
        NSLog(@"%@",error);
    }];
}
// 第三方登录其实就是拿第三方 accessToken
// 友盟如何获取 accessToken

- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self completion:^(id result, NSError *error) {
        
        UMSocialUserInfoResponse *resp = result;

        self.userInfo = resp;
//        // 第三方登录数据(为空表示平台未提供)
//        // 授权数据
//        NSLog(@" uid: %@", resp.uid);
//        NSLog(@" openid: %@", resp.openid);
//        NSLog(@" accessToken: %@", resp.accessToken);
//        NSLog(@" refreshToken: %@", resp.refreshToken);
//        NSLog(@" expiration: %@", resp.expiration);
//        
//        // 用户数据
//        NSLog(@" name: %@", resp.name);
//        NSLog(@" iconurl: %@", resp.iconurl);
//        NSLog(@" gender: %@", resp.unionGender);
//        
//        // 第三方平台SDK原始数据
//        NSLog(@" originalResponse: %@", resp.originalResponse);
    }];
}

// 实例化友盟配置
- (void)installUMShare
{
    /* 打开调试日志 */
    [[UMSocialManager defaultManager] openLog:NO];
    
    /* 设置友盟appkey */
    [[UMSocialManager defaultManager] setUmSocialAppkey:USHARE_DEMO_APPKEY];
    
    [self configUSharePlatforms];
    
    [self confitUShareSettings];
}


- (void)confitUShareSettings
{
    /*
     * 打开图片水印
     */
    //[UMSocialGlobal shareInstance].isUsingWaterMark = YES;
    
    /*
     * 关闭强制验证https，可允许http图片分享，但需要在info.plist设置安全域名
     <key>NSAppTransportSecurity</key>
     <dict>
     <key>NSAllowsArbitraryLoads</key>
     <true/>
     </dict>
     */
    //[UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
    
}
- (void)configUSharePlatforms
{
    /*
     设置微信的appKey和appSecret
     [微信平台从U-Share 4/5升级说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_1
     */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx1173e230d93a4c40" appSecret:@"7ff0c03a40ad4efe10308ced30f095c5" redirectURL:@"http://www.uninhibited.cn"];
    /*
     * 移除相应平台的分享，如微信收藏
     */
    //[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
    
    /* 设置分享到QQ互联的appID
     * U-Share SDK为了兼容大部分平台命名，统一用appKey和appSecret进行参数设置，而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
     100424468.no permission of union id
     [QQ/QZone平台集成说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_3
     */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1106330875"/*设置QQ平台的appID*/  appSecret:@"dlxR9j1ELWGjjA5k" redirectURL:@"http://www.uninhibited.cn"];
    
    /*
     设置新浪的appKey和appSecret
     [新浪微博集成说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_2
     */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"2972017600"  appSecret:@"3d75cd06915930af08715300e7e17e70" redirectURL:@"http://www.uninhibited.cn"];
    
}


@end
